﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Web.UI;

namespace ASPNETVN.PORTAL.Modules.SITEMAP
{
    public partial class Main : PortalModuleControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];
                Guid id = Guid.NewGuid();
                string sitemapString = string.Empty;
                IList<Tab> topTabs = portal.Tabs.Where(x => x.ParentID == Guid.Empty).OrderBy(x => x.SortOrder).ToList<Tab>();
                sitemapString += "<div id='treeview'>";
                foreach (Tab tab in topTabs)
                {
                    if (PortalSecurity.IsInRoles(tab.AuthorizedRoles) || PortalSecurity.IsInRole("1"))
                    {
                        if (tab.ShowOnMenu || PortalSecurity.IsInRole("1"))
                        {
                            sitemapString += string.Format("<div><img src='{2}' border='0' />&nbsp;&nbsp;<a href='{0}'>{1}</a></div>", ResolveClientUrl("~/Default.aspx?tabid=" + tab.ID), tab.Name, ResolveClientUrl("~/Images/Icons/folder.gif"));
                            IList<Tab> childTabs = portal.Tabs.Where(x => x.ParentID == tab.ID).OrderBy(x => x.SortOrder).ToList<Tab>();

                            foreach (Tab childTab in childTabs)
                            {
                                if (PortalSecurity.IsInRoles(tab.AuthorizedRoles) || PortalSecurity.IsInRole("1"))
                                {
                                    if (tab.ShowOnMenu || PortalSecurity.IsInRole("1"))
                                    {
                                        sitemapString += string.Format("<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='{2}' border='0' />&nbsp;&nbsp;<a href='{0}'>{1}</a></span><br />", ResolveClientUrl("~/Default.aspx?tabid=" + childTab.ID), childTab.Name, ResolveClientUrl("~/Images/Icons/item.gif"));
                                    }
                                }
                            }
                        }
                    }
                }
                sitemapString += "</div>";
                divMain.Controls.Add(new LiteralControl(sitemapString));
            }
        }
    }
}